import wepy from 'wepy'
export default class extends wepy.mixin {
	data = {
		id: '1',
		pageIndex: '1',
		pageSize: '10',
		// 一级数据
		getCatesListInfo: [],
		// 二级数据
		getSecondlistInfo: [],
		//屏幕可用高度
		wh: 0,
		value: 0,
		active: 0,
		cid: '',
		urls: wepy.$instance.globalData.baseURl,
		zhanweit: '2020-05-07/202005071101421588820502650_d2l0h.png',
		scrollTop: 0,
		total: 1000,
	}
	methods = {
		//  点击切换时触发的事件
		onChange(e) {
			this.active = e.detail
			console.log(e.detail)
		},
		handle(id) {
			console.log('跳转到商品详情')
			wepy.navigateTo({
				url: '../../pages/goodsDatils/gooddatils?id=' + id,
			})
		},
		handle1(id) {
			console.log('点击了一级')
			console.log(id)
			this.cid = id
			this.pageIndex = '1'
			this.getSecondlistInfo = []
			this.getSecondlist(id)
			this.scrollTop = this.scrollTop - 1
		},
		// 右侧触底事件
		loadMore() {
			if (this.pageIndex * this.pageSize >= this.total) {
				return
			}
			let num = parseInt(this.pageIndex)
			num++
			this.pageIndex = num.toString()
			console.log('123', this.pageIndex)
			this.getSecondlist(this.cid)
		},
	}
	onLoad() {
		this.getSecondlistInfo = [];
		this.pageIndex = '1';
		this.getPhoneHeight()
		this.getCatesList()
	}
	onShow() {
		
	}
	// 获取屏幕的可用高度
	async getPhoneHeight() {
		const res = await wepy.getSystemInfo()
		console.log(res)
		if (res.errMsg === 'getSystemInfo:ok') {
			this.wh = res.windowHeight
		}
	}
	// 获取分类数据
	async getCatesList() {
		const res = await wepy.post('baseRest/getChildCategoryList', {
			categoryId: this.id,
		})
		console.log(res)
		if (res.statusCode != 200) {
			return wepy.noToast('获取数据失败')
		}
		this.getCatesListInfo = res.data.data
		this.cid = res.data.data[this.active].id
		this.getSecondlist(this.cid)
		this.$apply()
	}
	// 获取二级菜单数据
	async getSecondlist(id) {
		const res = await wepy.post('baseRest/getGoodsList', {
			categoryId: id,
			pageIndex: this.pageIndex,
			pageSize: this.pageSize,
		})
		console.log(res)
		if (res.statusCode != 200) {
			return wepy.noToast('获取数据失败')
		}
		this.getSecondlistInfo = [...this.getSecondlistInfo, ...res.data.data]
		this.$apply()
	}
}
